import java.util.AbstractQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class threadPool  {
    private static   int a=100;

    public static void main(String[] args) {


        //corePoolSize 核心线程数
        //maximumPoolSize 最大线程数
        //keepAliveTime 线程闲置超时时长
        //unit 指定 keepAliveTime 参数的时间单位
        //workQueue 任务队列
        ExecutorService executorService = new ThreadPoolExecutor(3, 10,
                0, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(512), // 使用有界队列，避免OOM
                new ThreadPoolExecutor.DiscardPolicy());


        executorService.execute(new run1());
        executorService.execute(new run1());
        executorService.execute(new run1());
        executorService.shutdown();
    }


   static class run1 implements Runnable{

        @Override
        public void run() {
            while (a>0) {
                //无线程安全
                //        if (i > 0) {
//            i--;
//            System.out.println(Thread.currentThread().getName() + "==========" + i);
//        }
                //保证线程安全
                synchronized (Object.class) {
                    if (a > 0) {
                        a--;
                        System.out.println(Thread.currentThread().getName() + "==========" + a);
                    }
                }
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
